﻿using Autofac;
using Pay.DTO;
using Pay.IService;
using Pay.TO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;

namespace Pay.Web.Areas.Admin.Controllers {
    [RouteArea("admin")]
    [RoutePrefix("tokenTransfer")]
    public class TokenTransferController : AdminController {
        private ITokenTransferService _iTokenTransferService;
        private IComponentContext _iocContext;
        public TokenTransferController(ITokenTransferService iTokenTransferService, IComponentContext iocContext) {
            _iTokenTransferService = iTokenTransferService;
            _iocContext = iocContext;
        }
        // GET: Admin/TokenTransfer
        public ActionResult Index() {
            return View();
        }
        /// <summary>
        /// 提现列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public JsonResult Gets(TokenTansferQuery query) {
            var model = _iTokenTransferService.Gets(query);
            return Json(ResultToJson.Json(model), JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 提现详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public JsonResult Get(long id, PageQuery queryPage) {
            var model = _iTokenTransferService.GetDetail(id, queryPage);
            return Json(ResultToJson.Json(model), JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 提现审核
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public JsonResult Audit(token_transfer_model model) {
            if (!ModelState.IsValid)
                return Json(ResultToJson.Json<bool>(ModelError));
            model.manager_id = currentUser.user_id;
            var result = _iTokenTransferService.Audit(model);
            return Json(ResultToJson.Json(result ? " 保存成功" : "保存失败", result));
        }
        public JsonResult UpdateBank(token_tranfer_bank_model model) {
            if (!ModelState.IsValid)
                return Json(ResultToJson.Json<bool>(ModelError));
            var result = _iTokenTransferService.UpdateBank(model);
            return Json(ResultToJson.Json(result ? " 保存成功" : "保存失败", result));
        }
        /// <summary>
        /// 下发
        /// </summary>
        /// <param name="id"></param>
        /// <param name="pay_channel"></param>
        /// <returns></returns>
        public async Task<JsonResult> Payoff(token_transfer_model model) {
            var iPayService = _iocContext.ResolveKeyed<IPayService>(model.pay_channel);
            var result = await iPayService.TokenTransfer(model.id);
            if (result.result_code == 1000) {
                if (result.status == 1 || result.status == 2) {
                    //真的转账时，要删除下面的代码
                    if (result.status == 2) {
                        _iTokenTransferService.UpdateEx(model.id, model.transaction_no);
                    }
                    return Json(ResultToJson.Json(result.result_msg, ""), JsonRequestBehavior.AllowGet);
                } else if (result.status == 3)
                    return Json(ResultToJson.Json<string>(result.result_msg), JsonRequestBehavior.AllowGet);
            }
            return Json(ResultToJson.Json<string>(result.result_msg), JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 单条代付查询
        /// </summary>
        /// <returns>The first.</returns>
        /// <param name="merchant_no">O单商商户号</param>
        /// <param name="order_no">Order no.</param>
        /// <param name="pay_channel">Pay channel.</param>
        [Route("transfer_first")]
        public async Task<JsonResult> TransferFirst(string merchant_no, string order_no, PayChannel pay_channel = PayChannel.ChPay) {
            var iPayService = _iocContext.ResolveKeyed<IPayService>(pay_channel);
            var query = new transfer_query { merchant_no = merchant_no, method = "6001", order_no = order_no, pay_channel = pay_channel };
            var result = await iPayService.TokenTransferQuery(query);
            if (result.result_code == 1000) {
                if (result.status == 1)
                    return Json(ResultToJson.Json("提现成功", ""), JsonRequestBehavior.AllowGet);
                return Json(ResultToJson.Json<string>(result.result_msg), JsonRequestBehavior.AllowGet);
            }
            return Json(ResultToJson.Json<string>(result.result_msg), JsonRequestBehavior.AllowGet);
        }
    }
}